package com.minyifang.wcspf.work.ui.home.fragments;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpFragment;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.ImageUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.Global;
import com.minyifang.wcspf.work.ui.home.model.MyCenterBean;
import com.minyifang.wcspf.work.ui.home.presenter.HomePage4Presenter;
import com.minyifang.wcspf.work.ui.home.view.HomePage4View;
import com.minyifang.wcspf.work.ui.user.activities.AddressActivity;
import com.minyifang.wcspf.work.ui.user.activities.DailySignActivity;
import com.minyifang.wcspf.work.ui.user.activities.LoginActivity;
import com.minyifang.wcspf.work.ui.user.activities.MemberInfoActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyCollectionActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyCommentActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyCouponActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyIntegralActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyOrderListActivity;
import com.minyifang.wcspf.work.ui.user.activities.MyWalletActivity;
import com.minyifang.wcspf.work.ui.user.activities.PriceChangeActivity;
import com.minyifang.wcspf.work.ui.user.activities.ServiceActivity;
import com.minyifang.wcspf.work.ui.user.activities.SettingActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页->我的
 * A simple {@link Fragment} subclass.
 */
public class HomePage4Fragment extends BaseMvpFragment<HomePage4View, HomePage4Presenter> implements HomePage4View {

    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_mobile)
    TextView tv_mobile;
    @BindView(R.id.tv_balance)
    TextView tv_balance;
    @BindView(R.id.tv_integral)
    TextView tv_integral;
    @BindView(R.id.tv_order1)
    TextView tv_order1;
    @BindView(R.id.tv_order2)
    TextView tv_order2;
    @BindView(R.id.tv_order3)
    TextView tv_order3;


    @BindView(R.id.sr_refresh)
    SmartRefreshLayout mRefreshLayout;

    public HomePage4Fragment() {
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_home_page4;
    }

    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    protected void onVisibilityChanged(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            getUserInfo();
        }
    }

    private void getUserInfo() {
        if (UserManager.getInstance().isLogin())
            getPresenter().getMyCenterPage();
    }

    @Override
    protected void initView() {
        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            if (UserManager.getInstance().isLogin())
                getPresenter().getMyCenterPage();
            else
                refreshLayout.finishRefresh();
        });
    }

    @Override
    protected void initData() {

    }


    /**
     * 登录
     */
    @OnClick(R.id.ll_login)
    void ll_login() {
        if (!Global.checkLogin(getContext())) {
            startActivity(new Intent(getContext(), LoginActivity.class));
        }
    }

    //0待付款、1待取货、2待收货、3订单完成、4订单取消、5订单退货.6待发货

    /**
     * 待支付
     */
    @OnClick(R.id.ll_wait_pay)
    void ll_wait_pay() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 1);
        }
    }


    /**
     * 待发货
     */
    @OnClick(R.id.ll_wait_send)
    void ll_wait_take() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 2);
        }
    }

    /**
     * 待收货
     */
    @OnClick(R.id.ll_wait_receive)
    void ll_wait_receive() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 3);
        }
    }

    /**
     * 查看所有订单
     */
    @OnClick(R.id.tv_browse_all_order)
    void tv_browse_all_order() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 0);
        }
    }

    /**
     * 我的余额
     */
    @OnClick(R.id.ll_my_wallet)
    void ll_my_wallet() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyWalletActivity.class));
    }

    /**
     * 我的积分
     */
    @OnClick(R.id.ll_my_integral)
    void ll_my_integral() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyIntegralActivity.class));
    }

    /**
     * 基本信息
     */
    @OnClick(R.id.ll_base_info)
    void ll_base_info() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MemberInfoActivity.class));
    }

    /**
     * 收货地址
     */
    @OnClick(R.id.ll_address)
    void ll_address() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), AddressActivity.class));
    }

    /**
     * 我的评价
     */
    @OnClick(R.id.ll_my_comment)
    void ll_my_comment() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCommentActivity.class));
    }

    /**
     * 我的优惠券
     */
    @OnClick(R.id.ll_my_coupon)
    void ll_my_coupon() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCouponActivity.class));
    }

    /**
     * 我的收藏
     */
    @OnClick(R.id.ll_my_collection)
    void ll_my_collection() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCollectionActivity.class));
    }

    /**
     * 联系客服
     */
    @OnClick(R.id.ll_contact_us)
    void ll_contact_us() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), ServiceActivity.class));
    }

    /**
     * 价格变动
     */
    @OnClick(R.id.ll_price_change)
    void ll_price_change() {
        if (Global.checkLogin(getContext()))
            PriceChangeActivity.start(getContext(), false);
    }

    /**
     * 签到
     */
    @OnClick(R.id.ll_daily_sign)
    void ll_daily_sign() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), DailySignActivity.class));
    }

    /**
     * 设置
     */
    @OnClick(R.id.iv_setting)
    void iv_setting() {
        startActivity(new Intent(getContext(), SettingActivity.class));
    }

    @Override
    protected HomePage4Presenter createPresenter() {
        return new HomePage4Presenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void showMyCenterPage(HttpRespond<String> respond) {
        mRefreshLayout.finishRefresh();
        if (respond.result == 1) {
            if (respond.isaes == 1) {
                LogUtil.e("个人中心数据: " + SafeUtils.decrypt(respond.data));
                MyCenterBean myCenterBean = new Gson().fromJson(SafeUtils.decrypt(respond.data), MyCenterBean.class);
                ImageUtils.getInstance().showImage( myCenterBean.getHead(), iv_head);
                tv_name.setText(myCenterBean.getNickname());
                tv_mobile.setText(myCenterBean.getMobile());
                tv_balance.setText(myCenterBean.getBalance());
                tv_integral.setText(myCenterBean.getIntegral());
                resetViewStyle();
                if (myCenterBean.getUnpaid() > 0) {
                    tv_order1.setVisibility(View.VISIBLE);
                    tv_order1.setText(String.valueOf(myCenterBean.getUnpaid()));
                }
                if (myCenterBean.getUnShipped() > 0) {
                    tv_order2.setVisibility(View.VISIBLE);
                    tv_order2.setText(String.valueOf(myCenterBean.getUnShipped()));
                }
                if (myCenterBean.getGoodsreceived() > 0) {
                    tv_order3.setVisibility(View.VISIBLE);
                    tv_order3.setText(String.valueOf(myCenterBean.getGoodsreceived()));
                }
            }
        }
        /*else if (respond.result == -1) {
            Global.reLogin(getActivity());
        }*/
        else
            ToastHelper.getInstance(getContext()).showWarn(respond.message);

    }

    private void resetViewStyle() {
        tv_order1.setVisibility(View.GONE);
        tv_order2.setVisibility(View.GONE);
        tv_order3.setVisibility(View.GONE);
    }
}
